package com.hzhc.controller.json;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.entity.*;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.*;
import com.hzhc.utils.CronUtil;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/8 11:00
 * @description JsonController
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/base/json")
public class JsonController extends BaseController {

    private final YwOrderService ywOrderService;
    private final YwOrderCntrService ywOrderCntrService;
    private final SysOrganizationService organizationService;
    private final CompanyInfoService companyInfoService;
    private final SysWxRoleService sysWxRoleService;
    private final SysYzsRoleService yzsRoleService;
    private final SysDictService sysDictService;
    private final CntrSizeService sizeService;
    private final CntrTypeService typeService;
    private final CntrYardService cntrYardService;
    private final CarInfoService carInfoService;
    private final CntrMainService mainService;
    private final CntrInfoService infoService;
    private final StationService stationService;
    private final SmsTemplateService templateService;
    private final SmsChannelService channelService;
    private final UserMemberService memberService;
    private final SysUserSecurityService securityService;
    private final SysAuthService authService;
    private final DeclareCarService declareCarService;



    /**
     * 获取Cron中文翻译
     *  @param json
     */
    @RequestMapping("/getCronEscape")
    public ResultUtil<String> getCronEscape(@RequestBody String json) {
        JSONObject object = JSONObject.parseObject(json);
        String executeCycle =  object.getString("executeCycle");
        return ResultUtil.build(CronUtil.translateToChinese(executeCycle), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 部门
     */
    @RequestMapping("/getSysOrganizationJson")
    public ResultUtil<List<SysOrganization>> getSysOrganizationJson(@RequestBody SysOrganization vo) {
        List<SysOrganization> list = organizationService.getSysOrganizationJson(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取客户信息分页列表
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getCompanyInfoVoJson")
    public ResultUtil<List<CompanyInfoVo>> getCompanyInfoVoJson(@RequestBody CompanyInfoVo vo) {
        Page<CompanyInfoVo> page = new Page<CompanyInfoVo>(vo.getPageIndex(), vo.getPageSize());
        List<CompanyInfoVo> companyInfoVoList = companyInfoService.getCompanyInfoVoJson(page, vo);
        return ResultUtil.build(companyInfoVoList,page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 客户信息选择下拉框 (不分页)
     * @param vo
     * @return
     */
    @RequestMapping("/getCompanyList")
    public ResultUtil<List<CompanyInfoVo>> getCompanyList(@RequestBody CompanyInfoVo vo){
        List<CompanyInfoVo> list = companyInfoService.getCompanyList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 小程序角色
     */
    @RequestMapping("/getSysWxRoleJson")
    public ResultUtil<List<SysWxRole>> getSysWxRoleJson(@RequestBody SysWxRoleVo vo) {
        Page<SysWxRole> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysWxRole> sysWxRoleList = sysWxRoleService.getSysWxRoleJson(page, vo);
        return ResultUtil.build(sysWxRoleList, page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 一站式角色
     */
    @RequestMapping("/getYzsRoleJsonPage")
    public ResultUtil<List<SysYzsRoleVo>> getYzsRoleJsonPage(@RequestBody SysYzsRoleVo vo) {
        Page<SysYzsRoleVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysYzsRoleVo> sysYzsRoleVoList = yzsRoleService.getYzsRoleJsonPage(page, vo);
        return ResultUtil.build(sysYzsRoleVoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取字典分页列表
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getSysDictVoJson")
    public ResultUtil<List<SysDictVo>> getSysDictVoJson(@RequestBody SysDictVo vo) {
        Page<SysDictVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysDictVo> sysDictVoList = sysDictService.getSysDictVoJson(page, vo);
        return ResultUtil.build(sysDictVoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 获取堆存区域接口
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrYardVoJson")
    public ResultUtil<List<CntrYardVo>> getCntrYardVoJson(@RequestBody CntrYardVo vo) {
        Page<CntrYardVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CntrYardVo> cntrYardVoJson = cntrYardService.getCntrYardVoJson(page, vo);
        return ResultUtil.build(cntrYardVoJson,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱尺寸list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrSizeList")
    public ResultUtil<List<CntrSizeVo>> getCntrSizeList(HttpServletRequest request,@RequestBody CntrSizeVo vo){
        List<CntrSizeVo> list = sizeService.getCntrSizeList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱类型list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrTypeList")
    public ResultUtil<List<CntrTypeVo>> getCntrSizeList(HttpServletRequest request,@RequestBody CntrTypeVo vo){
        List<CntrTypeVo> list = typeService.getCntrType(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    @RequestMapping("/getCntrSizeTypeList")
    public ResultUtil<List<CntrSizeVo>> getCntrSizeTypeList(HttpServletRequest request,@RequestBody CntrSizeVo vo){
        List<CntrSizeVo> list = sizeService.getCntrSizeTypeList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱场位区域 (一级下拉框)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrYardList")
    public ResultUtil<List<CntrYardVo>> getCntrYardList(HttpServletRequest request,@RequestBody CntrYardVo vo){
        List<CntrYardVo> list = cntrYardService.getCntrYard(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱场位贝位 (奇偶二级下拉框)
     * @param
     * @param area
     * @return
     */
    @RequestMapping("/getCntrYardArea2/{area}")
    public ResultUtil<List<CntrYard>> getCntrYardArea2(@PathVariable("area") String area){
        return ResultUtil.build(cntrYardService.selectBayByArea(area), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * *获取三级下拉箱位区域
     */
    @RequestMapping("/getCntrYardArea3/{area}/{bay}")
    public ResultUtil<List<String>> getCntrYardArea3(@PathVariable("area") String area,@PathVariable("bay") String bay){
        CntrYard cntrYard = cntrYardService.selectRowOrLayerBayArea(area,bay);
        String rowArray = cntrYard.getRowArray();
        JSONArray jsonArray = JSON.parseArray(rowArray);
        List<String> list  = jsonArray.toList(String.class);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱层数 (四级下拉框)
     * @param
     * @param area
     * @return
     */
    @RequestMapping("/getCntrYardArea4/{area}")
    public ResultUtil<List<String>> getCntrYardArea4(@PathVariable("area") String area){
        CntrYard yard = cntrYardService.selectRowByAreaAndBay(area);
        Integer layer =  Integer.valueOf(yard.getLayer());
        List<String> list = new ArrayList<String>();
        for (int i = 1; i <= layer; i++) {
            list.add("" + i);
        }
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 集装箱新增 箱主list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrMainList")
    public ResultUtil<List<CntrMainVo>> getCntrMainList(HttpServletRequest request,@RequestBody CntrMainVo vo){
        List<CntrMainVo> list = mainService.getCntrMain(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取已审核委托接口
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getYwOrderVoJson")
    public ResultUtil<List<YwOrderVo>> getYwOrderVoJson(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> ywOrderVos = ywOrderService.getYwOrderVoJson(page, vo);
        return ResultUtil.build(ywOrderVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取委托箱子信息
     *
     * @param vo
     * @return
     */
    @RequestMapping("/getYwOrderCntrVoJson")
    public ResultUtil<List<YwOrderCntrVo>> getYwOrderVoJson(@RequestBody YwOrderCntrVo vo) {
        Page<YwOrderCntrVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCntrVo> ywOrderVos = ywOrderCntrService.getYwOrderCntrVoJson(page, vo);
        return ResultUtil.build(ywOrderVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 车辆信息
     */
    @RequestMapping("/getCarJsonPage")
    public ResultUtil<List<CarInfo>> getCarJsonPage(@RequestBody CarInfoVo vo) {
        Page<CarInfo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CarInfo> carInfoList = carInfoService.getCarJsonPage(vo, page);
        return ResultUtil.build(carInfoList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 计费项目新增  计费要素/计费类型
     * @param code
     * @return
     */
    @GetMapping("/getChargeElement/{code}")
    public ResultUtil<List<SysDictVo>> getChargeElement(@PathVariable("code") String code){
        List<SysDictVo> list = sysDictService.getChargeElement(code);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 新增装车计划 站点选择
     * @param station
     * @return
     */
    @RequestMapping("/getStation")
    public ResultUtil<List<Station>> getStation(@RequestBody Station station){
        List<Station> list = stationService.getStation(station);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装车理货 车牌号选择
     * @param car
     * @return
     */
    @PostMapping("/getCarNoList")
    public ResultUtil<List<CarInfo>> getCarNoList(@RequestBody CarInfo car){
        List<CarInfo> list = carInfoService.getCarNoList(car);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取短信渠道
     * @param request
     * @param channel
     * @return
     */
    @RequestMapping("/getMsgChannel")
    public ResultUtil<List<SmsChannel>> getMsgChannel(HttpServletRequest request, @RequestBody SmsChannel channel){
        List<SmsChannel> list = channelService.getMsgChannel(channel);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 字典项 获取短信类型
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getMsgTypeList")
    public ResultUtil<List<SysDictVo>> getMsgTypeList(HttpServletRequest request, @RequestBody SysDictVo vo){
        List<SysDictVo> list = sysDictService.getMsgTypeList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取短信模板
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getMsgTemplateList")
    public ResultUtil<List<SmsTemplateVo>> getMsgTemplateList(HttpServletRequest request, @RequestBody SmsTemplateVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        List<SmsTemplateVo> list = templateService.getMsgTemplateList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取发送短信的对象(公司人员)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCompanyUserList")
    public ResultUtil<List<UserMemberVo>> getCompanyUserList(HttpServletRequest request, @RequestBody UserMemberVo vo){
        List<UserMemberVo> list = memberService.getCompanyUserList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取散改集下的箱子
     * @return
     */
    @RequestMapping("/getSgjCntrMsg")
    public ResultUtil<List<YwOrderCntr>> getSgjCntrMsg(HttpServletRequest request,@RequestBody YwOrderCntr cntr){
        List<YwOrderCntr> list = ywOrderService.getSgjCntrMsg(cntr);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取散改集下的车
     * @return
     */
    @RequestMapping("/getSgjCarMsg")
    public ResultUtil<List<YwOrderCarVo>> getSgjCarMsg(HttpServletRequest request,@RequestBody YwOrderCarVo car){
        List<YwOrderCarVo> list = ywOrderService.getSgjCarMsg(car);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 检测密码强度(弱、中、强)
     * @param vo
     * @return
     */
    @RequestMapping("/checkPasswordLevel")
    public ResultUtil<T> checkPasswordLevel(@RequestBody SysUserSecurityVo vo){
        return securityService.checkPasswordLevel(vo.getPassword());
    }

    /**
     * 获取库中启用的强度等级 和 提醒
     * @return
     */
    @GetMapping("/getStrengthLevel")
    public ResultUtil<List<SysUserSecurity>> getStrengthLevel(){
        return securityService.getStrengthLevel();
    }

    //司机集合
    @GetMapping("/getDriverList")
    public ResultUtil<List<UserMember>> getDriverList(){
        List<UserMember> list = memberService.getDriverList();
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //司机管理-车队选择列表
    @GetMapping("/getDriverCompanyList")
    public ResultUtil<List<CompanyInfo>> getDriverCompanyList(){
        List<CompanyInfo> list = companyInfoService.getDriverCompanyList();
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //业务 授权给车队
    @PostMapping("/addYwAuth")
    public ResultUtil<T> addYwAuth(HttpServletRequest request,@RequestBody SysAuthVo vo){
        return authService.addYwAuth(request,vo);
    }

    //当前业务 所授权 的车队
    @RequestMapping("/getAuthToCom")
    public ResultUtil<List<SysAuth>> getAuthToCom(HttpServletRequest request,@RequestBody SysAuthVo vo){
        List<SysAuth> list = authService.getAuthToCom(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //cms 车辆预约 导入 获取委托号
    @RequestMapping("/getYwOrderNoByType")
    public ResultUtil<List<YwOrderVo>> getYwOrderNoByType(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        List<YwOrderVo> list  = ywOrderService.getYwOrderNoByType(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //车辆预约--查询公司下车牌
    @RequestMapping("/getCarNoByCom")
    public ResultUtil<List<CarInfo>> getCarNoByCom(HttpServletRequest request,@RequestBody CarInfo info){
        List<CarInfo> list = carInfoService.getCarNoByCom(info);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //获取已进卡 没有业务号的卡口记录car_no车牌号
    @RequestMapping("/getCarNoJk")
    public ResultUtil<List<DeclareCarVo>> getCarNoJk(HttpServletRequest request,@RequestBody DeclareCarVo car){
        List<DeclareCarVo> list =  declareCarService.getCarNoJk(car);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

}
